package com.huabo.contract.service.impl;

import com.hbfk.util.PageInfo;
import com.huabo.contract.config.DateBaseConfig;
import com.huabo.contract.oracle.entity.TblContractInvoicesmanagemen;
import com.huabo.contract.oracle.mapper.TblContractInvoicesmanagemenMapper;
import com.huabo.contract.service.TblContractInvoicesmanagemenService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Service
public class TblContractInvoicesmanagemenServiceImpl implements TblContractInvoicesmanagemenService {

    @Resource
    private TblContractInvoicesmanagemenMapper tblContractInvoicesmanagemenMapper;

    @Override
    public void findInvoiceInfoListForCollection(PageInfo<TblContractInvoicesmanagemen> pageInfo, TblContractInvoicesmanagemen invoice) {
        if(DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            pageInfo.setTlist(tblContractInvoicesmanagemenMapper.findInvoiceInfoListForCollection(pageInfo, invoice));
            pageInfo.setTotalRecord(tblContractInvoicesmanagemenMapper.findInvoiceInfoListForCollectionCount(invoice));
            resultMap.put("code", "1");
            resultMap.put("msg", "访问接口成功");
            resultMap.put("pageInfo", pageInfo);
        } else {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            pageInfo.setTlist(tblContractInvoicesmanagemenMapper.findInvoiceInfoListForCollection(pageInfo, invoice));
            pageInfo.setTotalRecord(tblContractInvoicesmanagemenMapper.findInvoiceInfoListForCollectionCount(invoice));
            resultMap.put("code", "1");
            resultMap.put("msg", "访问接口成功");
            resultMap.put("pageInfo", pageInfo);
        }
    }

    @Override
    public void findContractInvociesManaeMenPageInfo(PageInfo<TblContractInvoicesmanagemen> pageInfo, TblContractInvoicesmanagemen invoice) {
        if(DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            pageInfo.setTlist(tblContractInvoicesmanagemenMapper.findContractInvociesManaeMenPageInfo(pageInfo, invoice));
            pageInfo.setTotalRecord(tblContractInvoicesmanagemenMapper.findContractInvociesManaeMenPageInfoCount(invoice));
            resultMap.put("code", "1");
            resultMap.put("msg", "访问接口成功");
            resultMap.put("date", pageInfo);
        } else {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            pageInfo.setTlist(tblContractInvoicesmanagemenMapper.findContractInvociesManaeMenPageInfo(pageInfo, invoice));
            pageInfo.setTotalRecord(tblContractInvoicesmanagemenMapper.findContractInvociesManaeMenPageInfoCount(invoice));
            resultMap.put("code", "1");
            resultMap.put("msg", "访问接口成功");
            resultMap.put("date", pageInfo);
        }
    }

    @Override
    public TblContractInvoicesmanagemen findInvoiceInfoByInvoiceId(BigDecimal invoiceid) {
        if(DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            return this.tblContractInvoicesmanagemenMapper.getInvoiceid(invoiceid);
        } else {
            return this.tblContractInvoicesmanagemenMapper.getInvoiceid(invoiceid);
        }
    }

    @Override
    public String saveInvoicemanageMen(TblContractInvoicesmanagemen oldInvoice) {
        if(DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            String result = null;
            Integer count = this.tblContractInvoicesmanagemenMapper.selectListCount(oldInvoice);
            if (oldInvoice.getInvoiceid() != null) {
                if (count > 0) {
                    result = "-1";
                } else {
                    this.tblContractInvoicesmanagemenMapper.updateMergenEntity(oldInvoice);
                    result = oldInvoice.getInvoiceid().toString();
                }
            } else {
                this.tblContractInvoicesmanagemenMapper.saveMergenEntity(oldInvoice);
                result = oldInvoice.getInvoiceid().toString();
            }


            return result;
        } else {
            String result = null;
            Integer count = this.tblContractInvoicesmanagemenMapper.selectListCount(oldInvoice);
            if (oldInvoice.getInvoiceid() != null) {
                if (count > 0) {
                    result = "-1";
                } else {
                    this.tblContractInvoicesmanagemenMapper.updateMergenEntity(oldInvoice);
                    result = oldInvoice.getInvoiceid().toString();
                }
            } else {
                this.tblContractInvoicesmanagemenMapper.saveMergenEntity(oldInvoice);
                result = oldInvoice.getInvoiceid().toString();
            }


            return result;
        }
    }

    @Override
    public void modifyInvoiceStatus(Integer invoiceId, Integer status) {
        if(DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            this.tblContractInvoicesmanagemenMapper.updateModifyInvoiceStatus(invoiceId, status);
        } else {
            this.tblContractInvoicesmanagemenMapper.updateModifyInvoiceStatus(invoiceId, status);
        }
    }

    @Override
    public void removeInvoiceInfo(Integer invoiceId) {
        if(DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            this.tblContractInvoicesmanagemenMapper.removeInvoiceInfo(invoiceId);
        } else {
            this.tblContractInvoicesmanagemenMapper.removeInvoiceInfo(invoiceId);
        }
    }

    @Override
    public void saveInvoicemanageMenTci(TblContractInvoicesmanagemen tci) {
        if(DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            this.tblContractInvoicesmanagemenMapper.saveInvoicemanageMenTci(tci);
        } else {
            this.tblContractInvoicesmanagemenMapper.saveInvoicemanageMenTci(tci);
        }
    }
}
